package reduce.fz.com.reduce.custom;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.animation.AlphaAnimation;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.service.UserService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.ServiceBuilder;

/**
 * Created by qingshengzheng on 15/9/7.
 */
public abstract class BaseFollowView extends TextView {
    private UserService userService = ServiceBuilder.builderUserService(null);
    /**
     *  用户自己
     */
    private User me;
    /**
     * 别的用户
     */
    private User owner;
    /**
     * 自己的uid
     */
    private Long mid;
    /**
     * 别人的uid
     */
    private Long uid;
    /**
     * 他是否关注了你
     */
    private Boolean is_leader = false;
    /**
     * 你是否关注了他
     */
    private Boolean is_follower = false;

    public void setFollowInterface(FollowInterface followInterface) {
        this.followInterface = followInterface;
    }

    private FollowInterface followInterface;

    public BaseFollowView(Context context) {
        super(context);
    }

    public BaseFollowView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseFollowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BaseFollowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }



    public void setStatus(Long mid, User owner) {
        this.mid = mid;
        this.owner = owner;
        this.uid = owner.uid;
        this.is_follower = owner.userAttach.isfollow;
        this.is_leader = owner.userAttach.isleader;
        resetStatus();
    }
    //没有关注状态
    public abstract void nofollowStatus();

    //已关注状态
    public abstract void followStatus();

    /**
     * 重置按钮状态
     *
     */
    public void resetStatus() {
        if (uid != null && uid.equals(mid)) {
            setVisibility(GONE);
            return;
        } else {
            setVisibility(VISIBLE);
        }
        if (owner.userAttach.isfollow) {//关注了他，他没关注我
            followStatus();
            setSelected(true);
        } else { //有可能互相没关注，有可能他关注了我

            nofollowStatus();
            setSelected(false);
        }
        setVisibility(VISIBLE);
    }


    public boolean doToggleAction() {
        User me = UserPreference.getUser();
        if (!owner.userAttach.isfollow) {//发出关注请求
            if (me != null) {
                me.userAttach.follow_num++;
            }
            owner.userAttach.isfollow = true;
            resetStatus();
            if (followInterface != null) {
                followInterface.onFollow(uid);
            }
            userService.follow(owner.uid, true, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
            }) {
                @Override
                public void error(ServiceException e) {

                }

                @Override
                public void responseInfo(Result<HashMap> data) {

                }
            });
        } else { //发出取消关注请求
            if (me != null) {
                me.userAttach.follow_num--;
            }
            owner.userAttach.isfollow = false;
            resetStatus();
            userService.follow(owner.uid, false, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
            }) {
                @Override
                public void error(ServiceException e) {
                }
                @Override
                public void responseInfo(Result<HashMap> data) {

                }
            });
            if (followInterface != null) {
                followInterface.onDisFollow(uid);
            }
        }
        return true;
    }


    public static interface FollowInterface {

        public void onFollow(Long uid);

        public void onDisFollow(Long uid);
    }

}
